#include <stdio.h>
#include <lwip/err.h>
#include <lwip/ip_addr.h>
#include <lwip/dns.h>  // 引入DNS相关头文件
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "lvgl.h"
#include "lv_port.h"
#include "lv_demos.h"
#include "st7789_driver.h"
#include "driver/gpio.h"
#include "nvs_flash.h"
#include "nvs.h"
#include "gui_guider.h"
#include "custom.h"
#include "simple_wifi_sta.h"
#include "http_weather.h"
#include "ntp_time.h"
#include "qmi8658c.h"
#include "PipeLine.h"

/* ---------- 堆栈大小 ---------- */
#define WIFITask_STACK_SIZE 4096
#define HTTPTask_STACK_SIZE 8192
#define STNPTask_STACK_SIZE 4096
#define LVGLTASK_STACK_SIZE 16384
#define BacklightTask_STACK_SIZE 2048
#define MonitoringTask_STACK_SIZE 2048

lv_ui guider_ui EXT_RAM_BSS_ATTR;

/* ---------- 静态栈/TCB（使用外部PSRAM） ---------- */
static StackType_t wifi_task_stack [WIFITask_STACK_SIZE / sizeof(StackType_t)] EXT_RAM_BSS_ATTR;
static StaticTask_t wifi_task_tcb EXT_RAM_BSS_ATTR;

static StackType_t sntp_task_stack [STNPTask_STACK_SIZE / sizeof(StackType_t)] EXT_RAM_BSS_ATTR;
static StaticTask_t sntp_task_tcb EXT_RAM_BSS_ATTR;

static StackType_t lvgl_task_stack [LVGLTASK_STACK_SIZE / sizeof(StackType_t)] EXT_RAM_BSS_ATTR;
static StaticTask_t lvgl_task_tcb EXT_RAM_BSS_ATTR;

static StackType_t http_task_stack [HTTPTask_STACK_SIZE / sizeof(StackType_t)] EXT_RAM_BSS_ATTR;
static StaticTask_t http_task_tcb EXT_RAM_BSS_ATTR;

static StackType_t backlight_task_stack [BacklightTask_STACK_SIZE / sizeof(StackType_t)] EXT_RAM_BSS_ATTR;
static StaticTask_t backlight_task_tcb EXT_RAM_BSS_ATTR;

// LVGL任务函数
void LVGLTask(void *pvParameters) 
{
    xEventGroupWaitBits(xEventGroup, NTP_DONE_BIT, pdTRUE, pdFALSE, pdMS_TO_TICKS(15000));
    printf("LVGL done\n");
   
    setup_ui(&guider_ui);
    while(1)
    {
        lv_task_handler(); //开始任务调度
        vTaskDelay(pdMS_TO_TICKS(15));
    }
}

void app_main(void) 
{  
    // NVS初始化（WIFI底层驱动有用到NVS，所以这里要初始化）
    nvs_flash_init();

    // 初始化显示驱动
    lv_port_init();

    // 初始化陀螺仪驱动
    // qmi8658c_init();

    // 设置ST7789 LCD背光
    st7789_lcd_backlight(1);

    // 任务调整队列初始化
    create_events();

     /* 1. Wi-Fi */
    xTaskCreateStaticPinnedToCore(
        wifi_task, "wifi_task",
        WIFITask_STACK_SIZE / sizeof(StackType_t), NULL, 4,
        wifi_task_stack, &wifi_task_tcb, 0);

    /* 2. SNTP */
    xTaskCreateStaticPinnedToCore(
        sntp_task, "sntp_task",
        STNPTask_STACK_SIZE / sizeof(StackType_t), NULL, 3,
        sntp_task_stack, &sntp_task_tcb, 1);

    /* 3. LVGL */
    xTaskCreateStaticPinnedToCore(
        LVGLTask, "Core0Task",
        LVGLTASK_STACK_SIZE / sizeof(StackType_t), NULL, 6,
        lvgl_task_stack, &lvgl_task_tcb, 1);

    /* 4. HTTP 天气 */
    xTaskCreateStaticPinnedToCore(
        http_test_task, "weather_task",
        HTTPTask_STACK_SIZE / sizeof(StackType_t), NULL, 4,
        http_task_stack, &http_task_tcb, 0);

    /* 5. 休眠控制 */
    xTaskCreateStaticPinnedToCore(
        backlight_control_task, "backlight_task",
        BacklightTask_STACK_SIZE / sizeof(StackType_t), NULL, 5,
        backlight_task_stack, &backlight_task_tcb, 0);
}




